package com.gy.assistant.ui.friend;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMBlacklistQueriedCallback;
import im.zego.zim.callback.ZIMBlacklistUsersRemovedCallback;
import im.zego.zim.entity.ZIMBlacklistQueryConfig;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMErrorUserInfo;
import im.zego.zim.entity.ZIMUserInfo;
import com.gy.assistant.R;
import com.gy.assistant.ui.friend.adapter.BlacklistListAdapter;
import com.gy.assistant.util.AppLogger;

import java.util.ArrayList;

public class ZIMBlacklistActivity extends Activity {

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ZIMBlacklistActivity.class);
        context.startActivity(intent);
    }

    BlacklistListAdapter blacklistListAdapter;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacklist);

        RecyclerView conversationRecyclerView = findViewById(R.id.blacklist);

        LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        conversationRecyclerView.setLayoutManager(linearLayoutManager);
        blacklistListAdapter = new BlacklistListAdapter();
        conversationRecyclerView.setItemAnimator(new DefaultItemAnimator());
        conversationRecyclerView.setAdapter(blacklistListAdapter);

        ZIMBlacklistQueryConfig config = new ZIMBlacklistQueryConfig();
        config.count = 3000;

        ZIM.getInstance().queryBlacklist(
            config, callback );

        blacklistListAdapter.setOnItemLongClickListener(new BlacklistListAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(ZIMUserInfo userInfo) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ZIMBlacklistActivity.this);
                String[] item = new String[] {
                        "DELETE",

                };
                builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ArrayList<String> userIDs = new ArrayList<>();
                        userIDs.add(userInfo.userID);
                        ZIM.getInstance().removeUsersFromBlacklist(userIDs, new ZIMBlacklistUsersRemovedCallback() {
                            @Override
                            public void onBlacklistUsersRemoved(ArrayList<ZIMErrorUserInfo> errorUserInfoArrayList, ZIMError zimError) {
                                AppLogger.getInstance().d(
                                        "onBlacklistUsersRemoved errorUserInfoArrayList: %s, zimError: %s",
                                        new Gson().toJson(errorUserInfoArrayList),  new Gson().toJson(zimError));
                                Toast.makeText(ZIMBlacklistActivity.this,  zimError.message, Toast.LENGTH_LONG).show();
                                ZIM.getInstance().queryBlacklist(config, callback );
                            }
                        });
                        dialog.dismiss();
                    }
                });
                builder.show();
            }
        });

    }

    ZIMBlacklistQueriedCallback callback = new ZIMBlacklistQueriedCallback() {
        @Override
        public void onBlacklistQueried(ArrayList<ZIMUserInfo> blacklist, int nextFlag, ZIMError zimError) {
            AppLogger.getInstance().d(
                    "onBlacklistQueried blacklist: %s, nextFlag: %d, zimError: %s",
                    new Gson().toJson(blacklist), nextFlag,    new Gson().toJson(zimError));
            blacklistListAdapter.addAll(blacklist);
        }


    };
}
